<?php
class ContactController extends TN_Controller_Action_Public
{
    const RESPONSE_PAGE = 21;
  
	public function indexAction()
	{
		$this->view->title = $this->view->t->_("Contacto");
		$form = $this->contactForm();
 		$this->view->form = $form;	
   	    
 		if (!$this->getRequest()->isPost())
    	{
       		return;
     	}
    	 else
    	 {
    		if (!$form->isValid($_POST))
    	  	{
				$this->view->form = $form;
				return;
			}
			else
			{
				$values = $form->getValues();
				$contact = new Zend_Models_Contact();
				$contact = $contact->createRow();
				$contact->name = $values['name'];
				$contact->last_name = $values['last_name'];
				$contact->email = $values['email'];
				$contact->comment = $values['comment'];
				$contact->date = date("Y-m-d");
				$contact->save();
				$this->sendEmail($values);
				$content = new Zend_Models_LangContent();
        	   	$this->_redirect($content->getContentUrl(self::RESPONSE_PAGE,Common_Translate::getLanguage()));
			}
		}
	}
	
    private function sendEmail($values)
    {
    	$conf = TN_Config::getInstance(TN_INSTANCE_SERVER);
        $this->view->nombre = $values['name'];
        $this->view->apellido = $values['last_name'];
		$this->view->email = $values['email'];
		$this->view->comentario = $values['comment'];
		$this->view->url = $conf->webhost;
        $body = $this->view->render("/contact/email.phtml");
		        
        $smtpemail = new Common_System_Mail();
        $smtpemail->addTo("pablo.barrenechea@gmail.com");
        $smtpemail->setFrom($values['email'],$values['name']." ".$values['last_name'] );
        $smtpemail->setBodyHtml($body,"utf-8");
        $smtpemail->setSubject("Trattoria Napolitana - Contacto");
        
        $smtpemail->sendUsingSMTP($conf->email->smtp->host, $conf->email->smtp->username, $conf->email->smtp->password);

    }
	
	private function contactForm()
	{
		$form = new Zend_Form();
 		$form->setAction("/contact")
 				->setMethod('post');
 		$form->setAttrib("class", "jNice");   	
 		$name = $form->createElement('text', 'name');
 		$name->setAttrib("class", "text-long");
 	   	$name->addValidator('NotEmpty')->setLabel($this->view->t->_('Nombre'))->setRequired(true);
		$name->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));	
		$form->addElement($name);
		
		$last_name = $form->createElement('text', 'last_name');
		$last_name->setAttrib("class", "text-long");
		$last_name->addValidator('NotEmpty')->setLabel($this->view->t->_('Apellido'))->setRequired(true);
		$last_name->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));	
		$form->addElement($last_name);

		$email = $form->createElement('text','email');
		$email->setAttrib("class", "text-long");
		$email->addValidator('emailAddress')->setLabel($this->view->t->_('E-mail'))->setRequired(true);
		$email->addErrorMessage($this->view->t->_('Direccion_de_mail_no_valida'));
		$form->addElement($email);
			
		$content1 = $form->createElement('textarea','comment');
		$content1->setAttrib("class", "text-long");
		$content1->setLabel($this->view->t->_("Contenido"));
		$content1->setRequired(true);
		$content1->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));	
		$form->addElement($content1);
		
		$form->addElement('submit', 'add', array('label' => $this->view->t->_('Aceptar')));
		return $form;
	}
}